﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using DataAccess;
using System.Data.SqlClient;

public partial class Manager_EditQuestion : System.Web.UI.Page
{
    string QuestionID = "";
    DataTable table;
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            QuestionID = Request.QueryString["ID"];
            DbCommand cmd = GenericData.CreateCommand();
            cmd.CommandText = "SELECT * FROM Question WHERE ID = " + QuestionID;
            table = GenericData.ExecuteReader(cmd);
            if (table.Rows.Count > 0)
            {
                DbCommand cmdCategory = GenericData.CreateCommand();
                cmdCategory.CommandText = "SELECT * FROM Category";

                DataTable tableCategory = GenericData.ExecuteReader(cmdCategory);
                ddlCategory.DataSource = tableCategory;
                ddlCategory.DataValueField = "ID";
                ddlCategory.DataTextField = "CategoryName";
                ddlCategory.DataBind();

                txtQuestion.Text = table.Rows[0]["Content"].ToString();
                string Mark = table.Rows[0]["Mark"].ToString();
                string CategoryID = table.Rows[0]["CategoryID"].ToString();

                ddlCategory.SelectedValue = CategoryID;

                DbCommand cmdAnswer = GenericData.CreateCommand();
                cmdAnswer.CommandText = "SELECT * FROM Answer WHERE QuestionID = " + QuestionID + " ORDER BY ID ASC";
                DataTable tableAnswer = GenericData.ExecuteReader(cmdAnswer);

                if (tableAnswer.Rows.Count > 0)
                {
                    txtAnswer1.Text = tableAnswer.Rows[0]["Content"].ToString();
                    if ((bool)tableAnswer.Rows[0]["IsTrue"] == true)
                    {
                        rdo1.Checked = true;
                    }
                    if ((bool)tableAnswer.Rows[1]["IsTrue"] == true)
                        rdo2.Checked = true;
                    if ((bool)tableAnswer.Rows[2]["IsTrue"] == true)
                        rdo3.Checked = true;
                    if ((bool)tableAnswer.Rows[3]["IsTrue"] == true)
                    {
                        rdo4.Checked = true;
                        Response.Write(tableAnswer.Rows[3]["IsTrue"].ToString());
                    }

                    txtAnswer2.Text = tableAnswer.Rows[1]["Content"].ToString();                   
                    txtAnswer3.Text = tableAnswer.Rows[2]["Content"].ToString();
                    txtAnswer4.Text = tableAnswer.Rows[3]["Content"].ToString();

                    idAnswer1.Text = tableAnswer.Rows[0]["ID"].ToString();
                    idAnswer2.Text = tableAnswer.Rows[1]["ID"].ToString();
                    idAnswer3.Text = tableAnswer.Rows[2]["ID"].ToString();
                    idAnswer4.Text = tableAnswer.Rows[3]["ID"].ToString();
                }
            }
            else
            {
                Response.Redirect("ManagerQuestion.aspx");
            }
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["ID"] == null || Request.QueryString["ID"] == "")
            Response.Redirect("ManagerQuestion.aspx");
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string QuestionContent = System.Web.HttpUtility.HtmlEncode(txtQuestion.Text);

            string CategoryID = ddlCategory.SelectedValue;
            string Mark = txtMark.Text;
            int typeQuestion = 0;
            if (rdoMultiple.Checked)
                typeQuestion = 1;

            Question question = new Question();
            question.Content = QuestionContent;
            question.CategoryID = int.Parse(CategoryID);
            question.Mark = float.Parse(Mark);
            question.Type = typeQuestion;
            question.ID = int.Parse(QuestionID);

            SqlTransaction transaction;
            SqlConnection scon = new SqlConnection("SERVER=.;DATABASE=AptitudeTest;UID=sa;PWD=1234$");
            scon.Open();
            transaction = scon.BeginTransaction();

            try
            {

                SqlCommand cmd = new SqlCommand("UPDATE Question SET CategoryID = " + question.CategoryID + ", Content = '" + question.Content + "', Type = " + question.Type + ", Mark = " + question.Mark + " WHERE ID = " + question.ID, scon, transaction);
                cmd.ExecuteNonQuery();

                string AnswerContent1 = System.Web.HttpUtility.HtmlEncode(txtAnswer1.Text);
                string AnswerContent2 = System.Web.HttpUtility.HtmlEncode(txtAnswer2.Text);
                string AnswerContent3 = System.Web.HttpUtility.HtmlEncode(txtAnswer3.Text);
                string AnswerContent4 = System.Web.HttpUtility.HtmlEncode(txtAnswer4.Text);

                if (rdo1.Checked)
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent1 + "', IsTrue = 1 WHERE ID = " + idAnswer1.Text, scon, transaction).ExecuteNonQuery();
                else
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent1 + "', IsTrue = 0 WHERE ID = " + idAnswer1.Text, scon, transaction).ExecuteNonQuery();

                if (rdo2.Checked)
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent2 + "', IsTrue = 1 WHERE ID = " + idAnswer2.Text, scon, transaction).ExecuteNonQuery();
                else
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent2 + "', IsTrue = 0 WHERE ID = " + idAnswer2.Text, scon, transaction).ExecuteNonQuery();

                if (rdo3.Checked)
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent3 + "', IsTrue = 1 WHERE ID = " + idAnswer3.Text, scon, transaction).ExecuteNonQuery();
                else
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent3 + "', IsTrue = 0 WHERE ID = " + idAnswer3.Text, scon, transaction).ExecuteNonQuery();

                if (rdo4.Checked)
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent4 + "', IsTrue = 1 WHERE ID = " + idAnswer4.Text, scon, transaction).ExecuteNonQuery();
                else
                    new SqlCommand("UPDATE Answer SET Content = '" + AnswerContent4 + "', IsTrue = 0 WHERE ID = " + idAnswer4.Text, scon, transaction).ExecuteNonQuery();

                transaction.Commit();
                Session.Add("Message", "Add question successfully");
            }
            catch (SqlException sqlError)
            {
                transaction.Rollback();
                Session.Add("Message", "<font color='red'>Error : " + sqlError.Message + ".Add question unsuccess</font>");
            }
            scon.Close();
            Response.Redirect("ManagerQuestion.aspx");
        }
    }
}